package com.tgy.servlet.user;

import com.tgy.entity.SmbmsUser;
import com.tgy.service.user.userServiceImpl.UserServiceImpl;
import com.tgy.utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/***
 * @ClassName: LoginServlet
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 9:39 2021/12/10
 * @version : V1.0
 */
public class LoginServlet extends HttpServlet {
    //控制层调用业务层

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取用户名和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //对比数据
        UserServiceImpl userService = new UserServiceImpl();
        SmbmsUser user = userService.login(userCode, userPassword);
        if (Objects.equals(user.getUserCode(),userCode) && Objects.equals(user.getUserPassword(),userPassword)){
            //用户信息放到Session中
            System.out.println("正确");
            req.getSession().setAttribute(Constant.USER_SESSION,user);
            //跳转到主页
            resp.sendRedirect("jsp/frame.jsp");
        }else{
            System.out.println("错误");
            //转发回登录页面,并提示错误信息
            req.setAttribute("error","用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }
}
